package com.bxs;

import com.bxs.config.IdsConfig;
import com.bxs.dao.DataSaveEngine;
import com.bxs.detector.DetectorEngine;
import com.bxs.handler.HandlerManager;
import com.bxs.pojo.DangerMsg;
import lombok.extern.slf4j.Slf4j;
import org.pcap4j.core.NotOpenException;
import org.pcap4j.core.PcapDumper;
import org.pcap4j.core.PcapPacket;
import org.pcap4j.packet.IpV4Packet;

import java.time.Instant;

/**
 * @author dream
 * @title: PacketHandlerRunner
 * @projectName ids_v0_2
 * @description: TODO
 */

// 数据包处理任务
@Slf4j
public class PacketHandlerRunner implements Runnable {
    public PcapPacket pcapPacket = null;

    // 数据包存储器
    public PcapDumper packetDumper = null;

    // 数据包处理器（对数据包进行日志记录等）
    public HandlerManager handlerManager = null;
    // 数据包检测器
    public DetectorEngine detectorEngine = null;
    // 处理结果记录器
    public DataSaveEngine dataSaveEngine = null;

    @Override
    public void run() {
        // 处理执行链
        if (handlerManager == null){
            log.error("not configure data handler linklist");
        }else{
            handlerManager.execute(pcapPacket);
        }
        //  检测器执行
        if(detectorEngine != null){
            DangerMsg res = detectorEngine.execute(pcapPacket);
            if(res != null){
                // 检测到危险数据包，进行数据记录
                System.out.println("detect danger packet...");
                dataSaveEngine.execute(res);
            }
        }
        log.info("task execute finished");
    }
}
